﻿(function () {
    var myApp = angular.module('MyControllers');

    // 考试结果管理 * * 计划列表 * * 的控制器及方法列表
    myApp.controller('reportPlanController', ['$scope', '$http', '$modal', 'examReportService', 'utils', function ($scope, $http, $modal, service, utils) {
        var srv = service.examPlanSrv;

        $scope.getList = function () {
            srv.getList($scope.planType).success(function (data) {
                $scope.list = data;
            });
        };

        $scope.official = function () {
            $scope.planType = 0;
            $scope.getList();
        };

        $scope.imatate = function () {
            $scope.planType = 1;
            $scope.getList();
        };

        $scope.detail = function (model) {
            $scope.loadingState = true;

            var parameter = {
                planId: model.batchId,
                planName: model.examPlanName
            };

            utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                if (data.resultType == 'Success') {
                    switch (model.examMode) {
                        case 1:
                        case 2:
                        case 4:
                            window.location.href = '#/examReport/tradExamList?parameter=' + data.appendData;
                            break;
                        case 3:
                            //window.location.href = '#/examReport/freeExamList?parameter=' + data.appendData;
                            window.location.href = '#/examReport/SubjectList?parameter=' + data.appendData;
                            break;
                        default:
                            utils.notify('不能确定考试类型，无法操作！', 'warning');
                            break;
                    }
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };

        $scope.official();
    }]);

    // 考试结果管理 * * 科目列表 * * 的控制器及方法列表
    myApp.controller('reportSubjectController', ['$scope', '$location', '$http', '$modal', 'examReportService', 'utils', function ($scope, $location, $http, $modal, service, utils) {
        var srv = service.freeExamSrv, planId, planName;
        $scope.currentPage = 1;
        $scope.pageSize = 10;

        $scope.init = function () {
            $scope.loadingState = true;

            utils.decrypt($location.$$search.parameter).success(function (data) {
                if (data.resultType == 'Success') {
                    var result = JSON.parse(data.appendData);
                    planId = result.planId;
                    planName = result.planName;

                    $scope.planName = planName;
                    $scope.getBuildStatusList();
                    $scope.getUploadStatusList();
                    $scope.loadList();

                    $scope.operList = [
                        { operId: 0, operName: '生成结果' }
                        ,{ operId: 1, operName: '上传结果' }
                        ,{ operId: 2, operName: '查看考生明细' }
                    ];
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };
        $scope.getBuildStatusList = function () {
            var data = [
                { buildId: null, buildName: '全部状态' },
                //{ buildId: null, buildName: '未生成' },// 未生成状态 查出的数据跟全部一样,所以隐藏了
                { buildId: 1, buildName: '已生成' }
            ];

            $scope.buildStatusList = data;
            $scope.curBuildStatus = $scope.buildStatusList[0];
        };
        $scope.getUploadStatusList = function () {
            var data = [
                { uploadId: null, uploadName: '全部状态' },
                { uploadId: 0, uploadName: '考试结果尚未生成' },
                { uploadId: 2, uploadName: '打包完成,等待上传' },
                { uploadId: 4, uploadName: '上传完成' }
            ];

            $scope.uploadStatusList = data;
            $scope.curUploadStatus = $scope.uploadStatusList[0];
        };
        $scope.query = function () {
            $scope.loadList();
        };
        $scope.loadList = function () {
            srv.getSubjectList(planId, $scope.currentPage, $scope.pageSize, $scope.curBuildStatus.buildId, $scope.curUploadStatus.uploadId).success(function (data) {
                $scope.totalPage = data.totalPage;// 总页数
                $scope.list = data;
            });
        };
        // 跳转页
        $scope.selectPage = function(){
            //不能小于1大于最大
            if ($scope.currentPage < 1) $scope.currentPage = 1;
            if ($scope.currentPage > $scope.totalPage) $scope.currentPage = $scope.totalPage;
            $scope.loadList();
        };
        // 上一页
        $scope.Previous = function(){
            if($scope.currentPage > 1){
                $scope.currentPage--;
                $scope.loadList();
            }
        };
        // 下一页
        $scope.Next = function(){
            if($scope.currentPage < $scope.totalPage){
                $scope.currentPage++;
                $scope.loadList();
            }
        };
        $scope.oper = function (model) {
            switch (model.curOper.operId) {
                case 0:
                    var modalInstance = $modal.open({
                        template: "<div style='text-align:center;'>生成结果中 <img src='../App/images/ExamPaper/loading.gif' style='width:20px;' /></div>",
                        size: "sm",// 小窗口 lg:大窗口
                        backdrop: 'static',
                        keyboard: false// 当按下Esc时，模态对话框是否关闭
                    });

                    srv.buildFreeExamReport(planId, model.$parent.item.examSubjectID).success(function (data) {
                        if (data.resultType == 'Success') {
                            $scope.query();// 生成后刷新当前页面
                            utils.notify('生成结果成功！', 'success');
                        } else {
                            utils.notify(data.message, 'warning');
                        }
                        modalInstance.close(true);// 关闭模态窗
                    });

                    break;
                case 1:// 上传结果
                    if(model.$parent.item.examResult == ""){
                        utils.notify("考试结果尚未生成,不能上传", 'warning');
                    }else{
                        $scope.uploadResult(model.$parent.item);
                    }

                    break;
                case 2:
                    var parameter = {planId: planId, planName: planName, subjectId: model.$parent.item.examSubjectID, subjectName: model.$parent.item.subjectName };
                    utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                        if (data.resultType == 'Success') {
                            window.location.href = '#/examReport/freeExamList?parameter=' + data.appendData;
                        } else {
                            utils.notify(data.message, 'warning');
                        }
                    });

                    break;
            }
        };
        $scope.check = function (model) {
            var cbx = $('#cbxSubject_' + model.examSubjectID + '_' + String($.trim(model.examineeID)));
            cbx.prop('checked', cbx.is(':disabled') ? false : !cbx.is(':checked'));
        };
        $scope.checkAll = function () {
            var isCheck = $('.table :checkbox:first').is(':checked');

            $('.table :checkbox').not(':first').each(function () {
                $(this).prop('checked', $(this).is(':disabled') ? false : isCheck);
            });
        };

        $scope.uploadResult = function (model) {
            $scope.loadingState = true;
            var checkList = [];

            if(model){// 单个
                checkList.push({ examReportId: model.examReportID, examResultUrl: model.examResult, batchId: model.examPlanInfoID, deploymentMode: model.deploymentMode });
            }else{// 批量
                angular.forEach($scope.list.items, function (model, index, array) {
                    var cbx = $('#cbxSubject_' + model.examSubjectID + '_' + String($.trim(model.examineeID)));

                    if (cbx.is(':checked') && !cbx.is(':disabled')) {
                        checkList.push({ examReportId: model.examReportID, examResultUrl: model.examResult, batchId: model.examPlanInfoID, deploymentMode: model.deploymentMode });
                    }
                });
            }

            if (checkList.length <= 0) {
                $scope.loadingState = false;
                utils.notify('请选择一项进行上传操作！', 'warning');
            }else{
                srv.uploadResultPackage(checkList).success(function (data) {
                    $scope.loadingState = false;

                    if (data.resultType == 'Success') {
                        $scope.query();// 上传后刷新当前页面
                        utils.notify("上传结果成功", 'warning');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };

        $scope.init();
    }]);

    // 考试结果管理 * * 统考情况 * * 的控制器及方法列表
    myApp.controller('reportTradExamController', ['$scope', '$location', '$http', '$modal', 'examReportService', 'utils', function ($scope, $location, $http, $modal, service, utils) {
        var srv = service.tradExamSrv, planId, planName;

        $scope.init = function () {
            $scope.loadingState = true;
            utils.decrypt($location.$$search.parameter).success(function (data) {
                if (data.resultType == 'Success') {
                    var result = JSON.parse(data.appendData);

                    planId = result.planId;
                    planName = result.planName;

                    $scope.planName = planName;
                    $scope.getSceneList();
                    $scope.getRoomList();
                    $scope.getSubjectList();
                    $scope.getStatusList();
                    $scope.loadList();
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };
        $scope.getSceneList = function (roomId, subjectId) {
            if (!$scope.isAnyTime) {
                srv.getSceneList(planId, roomId, subjectId).success(function (data) {
                    angular.forEach(data, function (item) {
                        item.examSceneName += ' (' + utils.formatDate(item.planBeginTime) + ')';
                    });

                    data.unshift({ examSceneID: null, examSceneName: '全部场次' });

                    $scope.sceneList = data;
                    $scope.curScene = $scope.sceneList[0];
                });
            }
        };
        $scope.getRoomList = function (sceneId, subjectId) {
            if (!$scope.isAnyTime) {
                srv.getRoomList(planId, sceneId, subjectId).success(function (data) {
                    data.unshift({ examRoomID: null, examRoomName: '全部考场' });

                    $scope.roomList = data;
                    $scope.curRoom = $scope.roomList[0];
                });
            }
        };
        $scope.getSubjectList = function (sceneId, roomId) {
            srv.getSubjectList(planId, sceneId, roomId).success(function (data) {
                data.unshift({ examSubjectID: null, subjectName: '全部科目' });

                $scope.subjectList = data;
                $scope.curSubject = $scope.subjectList[0];
            });
        };
        $scope.getStatusList = function () {
            $scope.statusList = [
                { statusId: null, statusName: '全部状态' },
                { statusId: 0, statusName: '未生成' },
                { statusId: 1, statusName: '已生成未上传' },
                { statusId: 4, statusName: '已上传' }
            ];
            $scope.curStatus = $scope.statusList[0];
        };
        $scope.query = function () {
            var curScene = $scope.curScene;
            var curRoom = $scope.curRoom;
            var curSubject = $scope.curSubject;
            var curStatus = $scope.curStatus;

            $scope.loadList(curScene.examSceneID, curRoom.examRoomID, curSubject.examSubjectID, curStatus.statusId);
        };
        $scope.loadList = function (sceneId, roomId, subjectId, statusId) {
            srv.getList(planId, sceneId, roomId, subjectId, statusId).success(function (data) {
                $scope.list = data;
            });
        };
        $scope.upload = function () {
            var selList = [];
            angular.forEach($scope.list, function (data, index, array) {
                var cbx = $('#cbx_' + data.examReportID);

                if (cbx.is(':checked') && !cbx.is(':disabled')) {
                    selList.push(data.examReportID);
                }
            });

            if (selList.length <= 0) {
                utils.notify('请选择一项进行上传操作！', 'warning');
            } else {
                $scope.loadingState = true;
                srv.upload(planId, selList).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        // 生成后刷新当前页面
                        $scope.query();
                        utils.notify('上传报告成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.download = function () {
            var selList = [];
            angular.forEach($scope.list, function (data, index, array) {
                var cbx = $('#cbx_' + data.examReportID);

                if (cbx.is(':checked') && !cbx.is(':disabled')) {
                    selList.push(data.examReportID);
                }
            });

            if (selList.length <= 0) {
                utils.notify('请选择一项进行导出操作！', 'warning');
            } else {
                $scope.loadingState = true;
                // srv.download(planId, selList).success(function (data) {
                //     $scope.loadingState = false;
                //     if (data.resultType == 'Success') {
                //         // 动态生成一个form，并通过它实现下载功能
                //         var form = $('<form></form>');
                //         form.attr('action', '/ExamReport/Download/');
                //         form.attr('method', 'post');
                //         form.css('display', 'none');
                //         var input = $('<input type="hidden" name="filePath" value="' + data.logMessage + '" />');
                //         form.append(input);
                //         form.appendTo('body');
                //         form.submit();
                //
                //         utils.notify('导出报告成功！', 'success');
                //     } else {
                //         utils.notify(data.message, 'warning');
                //     }
                // });

                srv.download(planId, selList).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        // 上传结果包
                        srv.uploadResultPackage(data.logMessage, function(uploadData){
                            utils.notify(uploadData.message, 'success');
                        });
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.detail = function (model) {
            $modal.open({
                templateUrl: '/App/views/ExamReport/TradExamDetail.html',
                controller: 'tradExamDetail',
                size: 'lg',
                resolve: {
                    params: function () {
                        return { model: model, planId: planId }
                    }
                }
            });
        };
        $scope.build = function (model) {
            $scope.check(model);

            $scope.loadingState = true;
            var loading = $('#loading_' + model.examReportID);
            loading.show();
            srv.buildExamReport(planId, model.examSceneID, model.examRoomId, model.examSubjectId).success(function (data) {
                $scope.loadingState = false;
                loading.hide();
                if (data.resultType == 'Success') {
                    // 生成后刷新当前页面
                    $scope.query();
                    utils.notify('生成报告成功！', 'success');
                } else {
                    utils.notify('生成报告失败！', 'warning');
                }
            });
        };
        $scope.check = function (model) {
            var cbx = $('#cbx_' + model.examReportID);
            cbx.prop('checked', cbx.is(':disabled') ? false : !cbx.is(':checked'));
        };
        $scope.checkAll = function () {
            var isCheck = $('.table :checkbox:first').is(':checked');

            $('.table :checkbox').not(':first').each(function () {
                $(this).prop('checked', $(this).is(':disabled') ? false : isCheck);
            });
        };
        $scope.signInReport = function () {
            $modal.open({
                templateUrl: '/App/views/ExamReport/SignInReport.html',
                controller: 'signInReport',
                resolve: {
                    params: function () {
                        return planId;
                    }
                }
            });
        }

        $scope.init();
    }]);

    // 考试结果管理 * * 统考详情 * * 的控制器及方法列表
    myApp.controller('tradExamDetail', ['$scope', '$routeParams', '$modalInstance', 'examReportService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        $scope.data = params.model;

        var srv = service.tradExamSrv;

        var methods = {
            init: function () {
                srv.getExamReportDetail(params.planId, $scope.data.examSceneID, $scope.data.examRoomId, $scope.data.examSubjectId).success(function (data) {
                    $scope.list = data;
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);

        $scope.init();
    }]);

    // 考试结果管理 * * 随考情况 * * 的控制器及方法列表
    myApp.controller('reportFreeExamController', ['$scope', '$location', '$http', '$modal', 'examReportService', 'utils', function ($scope, $location, $http, $modal, service, utils) {
        var srv = service.freeExamSrv, planId, planName, subjectId, subjectName;

        $scope.init = function () {
            $scope.loadingState = true;

            utils.decrypt($location.$$search.parameter).success(function (data) {
                if (data.resultType == 'Success') {
                    var result = JSON.parse(data.appendData);
                    planId = result.planId;
                    planName = result.planName;
                    subjectId = result.subjectId;
                    subjectName = result.subjectName;

                    $scope.subjectName = subjectName;
                    $scope.getList();
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };
        $scope.getList = function () {
            //$scope.getSubjectList();
            $scope.loadList();
        };
        $scope.getSubjectList = function () {
            service.tradExamSrv.getSubjectList(planId).success(function (data) {
                data.unshift({ examSubjectID: null, subjectName: '全部科目' });

                $scope.subjectList = data;
                $scope.curSubject = $scope.subjectList[0];
            });
        };
        $scope.query = function () {
            //var curSubject = $scope.curSubject;
            var curParams = $scope.curParams;
            $scope.loadList(curParams);
        };
        $scope.loadList = function (curParams) {
            srv.getList(planId, subjectId, curParams).success(function (data) {
                $scope.list = data;
            });
        };
        $scope.build = function () {
            var infoList = [];

            angular.forEach($scope.list, function (data, index, array) {
                var cbx = $('#cbxSubject_' + data.examSubjectID + '_' + String($.trim(model.examineeID)));

                if (cbx.is(':checked') && !cbx.is(':disabled')) {
                    infoList.push({ sceneId: data.examSceneID, roomId: data.examRoomID, subjectId: data.examSubjectID, examineeId: data.examineeID });
                }
            });

            if (infoList.length <= 0) {
                utils.notify('请至少选择一项进行生成操作！', 'warning');
            } else {
                $scope.loadingState = true;
                srv.buildFreeExamReport(planId, infoList).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        // utils.notify(data, 'warning');
                        // 生成后刷新当前页面
                        $scope.query();
                        utils.notify('生成报告成功！', 'success');
                    } else {
                        utils.notify('生成报告失败！', 'warning');
                    }
                });
            }
        };
        $scope.generated = function () {
            $scope.loadingState = true;

            var parameter = { planId: planId, planName: planName };
            utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                if (data.resultType == 'Success') {
                    window.location.href = '#/examReport/freeExamGenerated?parameter=' + data.appendData;
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };
        $scope.check = function (model) {
            var cbx = $('#cbxSubject_' + model.examSubjectID + '_' + String($.trim(model.examineeID)));
            cbx.prop('checked', cbx.is(':disabled') ? false : !cbx.is(':checked'));
        };
        $scope.checkAll = function () {
            var isCheck = $('.table :checkbox:first').is(':checked');

            $('.table :checkbox').not(':first').each(function () {
                $(this).prop('checked', $(this).is(':disabled') ? false : isCheck);
            });
        };
        $scope.signInReport = function () {
            $modal.open({
                templateUrl: '/App/views/ExamReport/SignInReport.html',
                controller: 'signInReport',
                resolve: {
                    params: function () {
                        return planId;
                    }
                }
            });
        }

        $scope.init();
    }]);

    // 考试结果管理 * * 生成历史 * * 的控制器及方法列表
    myApp.controller('reportFreeExamGeneratedController', ['$scope', '$location', '$http', '$modal', 'examReportService', 'utils', function ($scope, $location, $http, $modal, service, utils) {
        var planId, filePath,srv = service.freeExamSrv;

        $scope.init = function () {
            $scope.loadingState = true;
            utils.decrypt($location.$$search.parameter).success(function (data) {
                if (data.resultType == 'Success') {
                    var result = JSON.parse(data.appendData);
                    planId = result.planId;
                    $scope.planName = result.planName;
                    $scope.getList();
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        }
        $scope.getList = function () {
            srv.getGeneratedList(planId).success(function (data) {
                $scope.list = data;
            });
        };
        $scope.upload = function () {
            var selList = [];
            angular.forEach($scope.list, function (data, index, array) {
                var cbx = $('#cbx_' + data.examSubjectID);

                if (cbx.is(':checked') && !cbx.is(':disabled')) {
                    selList.push(data.examSubjectID);
                }
            });

            if (selList.length <= 0) {
                utils.notify('请选择一项进行上传操作！', 'warning');
            } else {
                $scope.loadingState = true;
                srv.upload(planId, selList).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        // 生成后刷新当前页面
                        $scope.init();
                        utils.notify('上传报告成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.download = function () {
            var selList = [];
            angular.forEach($scope.list, function (data, index, array) {
                var cbx = $('#cbx_' + data.ExamSubjectID);

                if (cbx.is(':checked') && !cbx.is(':disabled')) {
                    selList.push({ subjectId: data.ExamSubjectID, roomId: data.ExamRoomId });
                }
            });

            if (selList.length <= 0) {
                utils.notify('请选择一项进行导出操作！', 'warning');
            } else {
                $scope.loadingState = true;
                srv.download(planId, selList).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        // 动态生成一个form，并通过它实现下载功能
                        // var form = $('<form></form>');
                        // form.attr('action', '/ExamReport/Download/');
                        // form.attr('method', 'post');
                        // form.css('display', 'none');
                        // var input = $('<input type="hidden" name="filePath" value="' + data.logMessage + '" />');
                        // form.append(input);
                        // form.appendTo('body');
                        // form.submit();
                        srv.uploadResultPackage(data.logMessage).success(function (dataUpload) {
                            alert(dataUpload.message);
                        });
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.check = function (model) {
            var cbx = $('#cbx_' + model.examSubjectID);
            cbx.prop('checked', cbx.is(':disabled') ? false : !cbx.is(':checked'));
        };
        $scope.checkAll = function () {
            var isCheck = $('.table :checkbox:first').is(':checked');

            $('.table :checkbox').not(':first').each(function () {
                $(this).prop('checked', $(this).is(':disabled') ? false : isCheck);
            });
        }

        $scope.init();
    }]);

    // 考试结果管理 * * 签到报告 * * 的控制器及方法列表
    myApp.controller('signInReport', ['$scope', '$routeParams', '$modalInstance', 'examReportService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.examPlanSrv;

        $scope.init = function () {
            $scope.loadingState = true;
            srv.getSignInReport(params).success(function (data) {
                $scope.loadingState = false;
                $scope.list = data;
            });
        };
        $scope.close = function () {
            $modalInstance.close(true);
        };

        $scope.init();
    }]);
})();